<!-- xml version="1.0" encoding="utf-8" -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
	<title>Latin Nouns</title>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
	<link href="../classics.css" rel="stylesheet" type="text/css">
</head>

<?php
function endsWith($haystack, $needle)
{
    $length = strlen($needle);
    $start  = $length * -1; //negative
    return (substr($haystack, $start) === $needle);
}


$mfendings = array('Singular' => array('Nom.' => 'us', 'Gen.' => 'ūs', 'Dat.' => 'uī', 'Acc.' => 'um', 'Abl.' => 'ū'),
	               'Plural' => array('Nom.' => 'ūs', 'Gen.' => 'uum', 'Dat.' => 'ibus', 'Acc.' => 'ūs', 'Abl.' => 'ibus'));

$nendings = array('Singular' => array('Nom.' => 'ū', 'Gen.' => 'ūs', 'Dat.' => 'ū', 'Acc.' => 'ū', 'Abl.' => 'ū'),
	              'Plural' => array('Nom.' => 'ua', 'Gen.' => 'uum', 'Dat.' => 'ibus', 'Acc.' => 'ua', 'Abl.' => 'ibus'));


$nouns = array('manus', 'genū');

$Nom = $nouns[array_rand($nouns)];

if (endsWith($Nom, 'us')) {
	$stem = mb_substr($Nom, 0, -2, 'UTF-8');
	$endings = $mfendings;
} elseif (endsWith($Nom, 'ū')) {
	$stem = mb_substr($Nom, 0, -1, 'UTF-8');
	$endings = $nendings;
} else {
	exit("Wrong ending!");
}

$vtab = "<caption>".$Nom."</caption>\n";
$vtab = $vtab . "<tr><td></td><th>Singular</th><th>Plural</th></tr>\n";

foreach(array('Nom.', 'Gen.', 'Dat.', 'Acc.', 'Abl.') as $case) {
	$vtab = $vtab . "<tr><th>$case</th>";
	foreach(array('Singular', 'Plural') as $number) {
		$vtab = $vtab . "<td>";
		if (isset($endings[$number][$case])) {
			$txts[] = $stem.$endings[$number][$case];
			$vtab = $vtab . "<input name=\"text".(count($txts) - 1)."\" type=\"text\">";
		}
		$vtab = $vtab . "</td>";
	}
	$vtab = $vtab . "</tr>\n";
}
?>

<script type="text/javascript">
function check() 
{

<?php
	for($i = 0, $nall = count($txts); $i < $nall; $i++) {
		print "if (document.n01.text".$i.".value=='".$txts[$i]."')\n{\n";
		print "\tdocument.n01.text".$i.".disabled=true;\n}\n";
	}
?>

}

function newWord() 
{
<?php
	for($i = 0, $nall = count($txts); $i < $nall; $i++) {
		print "document.n01.text".$i.".disabled=false;\n";
 		print "document.n01.text".$i.".value='';\n";
	}
?>
	location.reload();
}
</script>

<body>
<div id="container">
<div id="center">
<form name="n01" action="">
<table align="center">
<?php
	print $vtab;
?>
<tr><td></td>
	<td align="center"><input name="Check" onclick="check()" value="Check" type="button"></td>
	<td align="center"><input name="New Word" onclick="newWord()" value="New Word" type="button"></td>
</tr>
</table>
</form>
</div>
</div>
</body>
</html>
